package com.example.aigc_education.integration.impl;

import com.example.aigc_education.integration.AgentServiceIntegration;
import com.example.aigc_education.integration.request.ChatMessageRequestV2;
import com.example.aigc_education.rpc.api.v2.ChatServiceGrpc;
import com.example.aigc_education.rpc.api.v2.ChatServiceProto;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AgentServiceIntegrationImpl implements AgentServiceIntegration {

    @GrpcClient("agent-algo-service")
    private ChatServiceGrpc.ChatServiceStub chatServiceStub;

    @Override
    public void stream(ChatMessageRequestV2 request, StreamObserver<ChatServiceProto.ChatMessage> responseObserver) {
        ChatServiceProto.ChatMessageContent chatMessageContent = ChatServiceProto.ChatMessageContent.newBuilder()
                .setType(request.getType())
                .setText(request.getContent().toString())
                .build();

        ChatServiceProto.ChatMessage chatMessage = ChatServiceProto.ChatMessage.newBuilder()
                .setUserId(Long.parseLong(request.getUserId()))
//                .setConvId(Long.parseLong(request.getConvId()))
                .setConvId(request.getConvId() == null ? null : Long.parseLong(request.getConvId()))
                .setRole(ChatServiceProto.Role.HUMAN_VALUE)
                .setMessageId(request.getMessageId())
                .addContents(chatMessageContent)
                .build();

        chatServiceStub.stream(chatMessage, responseObserver);
    }
}
